<?php


declare(strict_types=1);
/**
 * @Author: Frank
 * @Email:   frank_hhm@163.com
 * @Last Modified time: 2023-01-13 10:51:08
 */
namespace app\sys\controller\system;

use app\sys\controller\Base;
use think\facade\{
    App
};
use app\common\services\system\ConfigService;
use app\common\services\system\ConfigTabService;
/**
 * 配置项
 * Class BasicConfig 
 * @package app\sys\controller\system
 */
class BasicConfig extends Base
{
    /**
     * Config constructor.
     * @param App $app
     */
    public function __construct(App $app,ConfigTabService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 获取配置项
     * @method(GET)
     */
    public function index()
    {
        $data = $this->request->getMore([
            ['field', ''],
        ]);
        $this->success($this->service->getConfigByEngField($data['field']));
    }

    /**
     * 获取配置分类全部子级id
     * @noAuth(true)
     * @method(GET)
     */
    public function getTabSubIds()
    {
        $data = $this->request->getMore([
            ['tab_id', ''],
        ]);
        $this->success($this->service->getConfigTabChildIdsByTabId($data['tab_id']));
    }

    /**
     * 获取配置表单
     * @noAuth(true)
     * @method(GET)
     */
    public function getItems(){
        $data = $this->request->getMore([
            ['tab_id', ''],
        ]);
        $service = ConfigService::instance();
        $configItems = $service->getConfigItemsByTabId($data['tab_id']);
        $this->success(array_merge(['type'=>'from'],$configItems));
    }

    /**
     * 保存配置
     * @method(POST)
     */
    public function save()
    {
        $data = $this->request->post();
        foreach ($data as $key => $value) {
            if(!ConfigService::instance()->checkFieldRules($key,$value)){
                continue;
            }
            sysconf($key, $value);
        }
        $this->success('配置保存成功！');
    }
}